<?php
namespace app\admin\controller;
use app\admin\model\Link as LinkModel;
use app\admin\controller\Base;//权限验证
use think\Loader;
use think\Session;
class Link extends Base{
	
    public function index(){
		$model= new LinkModel();
		//栏目分页
		$list = LinkModel::paginate(5);
		$this->assign('list',$list);
		return $this->fetch('list');
	}
	
	public function add(){
		//栏目添加
		if(request()->isPost()){
			$data = [
				'title' => input('title'),
				'url' => input('url'),
				'desc' => input('desc'),
				'addtime'=>date("Y-m-d H:i:s"),
			];
			$validate = Loader::validate('Link');
			if(!$validate->check($data)){
			   $this->error($validate->getError()); 
			   die();
			}
			if(db('link')->insert($data)){
				return $this->success('添加链接成功！','index');
			}else{
				return $this->error('添加链接失败！');
			}
			return;
		}
		return $this->fetch('add');
	}
	
	
	public function edit(){
		//栏目名称修改
		$id=input('id');
		$links=db('link')->find($id);
		if(request()->isPost()){
			$data=[
				'id'=>input('id'),
				'title'=>input('title'),
				'url'=>input('url'),
				'desc'=>input('desc'),
				'addtime'=>date("Y-m-d H:i:s"),
			];
			$validate = Loader::validate('Link');
			if(!$validate->check($data)){
			   $this->error($validate->getError()); 
			   die();
			}
	        $save=db('link')->update($data);
			if($save !== false){
				$this->success('修改栏目名称成功！','index');
			}else{
				$this->error('修改栏目名称失败！');
			}
			return;
		}
		$this->assign('links',$links);
		return $this->fetch('edit');
	}
	
	
	public function del(){
		//栏目删除
		$id=input('id');
		
		if(db('link')->delete(input('id'))){
			$this->success('链接删除成功！','index');
		}else{
			$this->error('链接删除失败！');
		}
	}
	
	
}